#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
	int fd = -1;

	char buf[100] = {"hello\n"};

	fd = creat("test.data", 0777);
	if(fd < 0)
	{
		fd = open("test.data", O_RDONLY);
		read(fd, buf, 100);
		freopen("test.data", "r", stdin);		
	}
	else
	{
		write(fd, buf, 100);
		close(fd);
	}
}
